<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="This implements the Atari games with multi-processing."/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="Atari wrapper with multi-processing"/>
    <meta name="twitter:description" content="This implements the Atari games with multi-processing."/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/rl/game.html"/>
    <meta property="og:title" content="Atari wrapper with multi-processing"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="Atari wrapper with multi-processing"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="Atari wrapper with multi-processing"/>
    <meta property="og:description" content="This implements the Atari games with multi-processing."/>

    <title>Atari wrapper with multi-processing</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/rl/game.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="index.html">rl</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/rl/game.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            <h1>Atari wrapper with multi-processing</h1>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">9</span><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span>
<span class="lineno">10</span><span class="kn">import</span> <span class="nn">multiprocessing.connection</span>
<span class="lineno">11</span>
<span class="lineno">12</span><span class="kn">import</span> <span class="nn">cv2</span>
<span class="lineno">13</span><span class="kn">import</span> <span class="nn">gym</span>
<span class="lineno">14</span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-1'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-1'>#</a>
            </div>
            <p> <a id="GameEnvironment"></a></p>
<h2>Game environment</h2>
<p>This is a wrapper for OpenAI gym game environment. We do a few things here:</p>
<p>1. Apply the same action on four frames and get the last frame 2. Convert observation frames to gray and scale it to (84, 84) 3. Stack four frames of the last four actions 4. Add episode information (total reward for the entire episode) for monitoring 5. Restrict an episode to a single life (game has 5 lives, we reset after every single life)</p>
<h4>Observation format</h4>
<p>Observation is tensor of size (4, 84, 84). It is four frames (images of the game screen) stacked on first axis. i.e, each channel is a frame.</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">17</span><span class="k">class</span> <span class="nc">Game</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-2'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-2'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">38</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-3'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-3'>#</a>
            </div>
            <p>create environment </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">40</span>        <span class="bp">self</span><span class="o">.</span><span class="n">env</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">make</span><span class="p">(</span><span class="s1">&#39;BreakoutNoFrameskip-v4&#39;</span><span class="p">)</span>
<span class="lineno">41</span>        <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-4'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-4'>#</a>
            </div>
            <p>tensor for a stack of 4 frames </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">44</span>        <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-5'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-5'>#</a>
            </div>
            <p>buffer to keep the maximum of last 2 frames </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">47</span>        <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-6'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-6'>#</a>
            </div>
            <p>keep track of the episode rewards </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">50</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span> <span class="o">=</span> <span class="p">[]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-7'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-7'>#</a>
            </div>
            <p>and number of lives left </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">52</span>        <span class="bp">self</span><span class="o">.</span><span class="n">lives</span> <span class="o">=</span> <span class="mi">0</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-8'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-8'>#</a>
            </div>
            <h3>Step</h3>
<p>Executes <code  class="highlight"><span></span><span class="n">action</span></code>
 for 4 time steps and  returns a tuple of (observation, reward, done, episode_info).</p>
<ul><li><code  class="highlight"><span></span><span class="n">observation</span></code>
: stacked 4 frames (this frame and frames for last 3 actions) </li>
<li><code  class="highlight"><span></span><span class="n">reward</span></code>
: total reward while the action was executed </li>
<li><code  class="highlight"><span></span><span class="n">done</span></code>
: whether the episode finished (a life lost) </li>
<li><code  class="highlight"><span></span><span class="n">episode_info</span></code>
: episode information if completed</li></ul>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">54</span>    <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-9'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-9'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">66</span>        <span class="n">reward</span> <span class="o">=</span> <span class="mf">0.</span>
<span class="lineno">67</span>        <span class="n">done</span> <span class="o">=</span> <span class="kc">None</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-10'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-10'>#</a>
            </div>
            <p>run for 4 steps </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">70</span>        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-11'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-11'>#</a>
            </div>
            <p>execute the action in the OpenAI Gym environment </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">72</span>            <span class="n">obs</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
<span class="lineno">73</span>
<span class="lineno">74</span>            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">:</span>
<span class="lineno">75</span>                <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span><span class="p">[</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">)</span>
<span class="lineno">76</span>
<span class="lineno">77</span>            <span class="n">reward</span> <span class="o">+=</span> <span class="n">r</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-12'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-12'>#</a>
            </div>
            <p>get number of lives left </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">80</span>            <span class="n">lives</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">unwrapped</span><span class="o">.</span><span class="n">ale</span><span class="o">.</span><span class="n">lives</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-13'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-13'>#</a>
            </div>
            <p>reset if a life is lost </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">82</span>            <span class="k">if</span> <span class="n">lives</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">lives</span><span class="p">:</span>
<span class="lineno">83</span>                <span class="n">done</span> <span class="o">=</span> <span class="kc">True</span>
<span class="lineno">84</span>                <span class="k">break</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-14'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-14'>#</a>
            </div>
            <p>maintain rewards for each step </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">87</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reward</span><span class="p">)</span>
<span class="lineno">88</span>
<span class="lineno">89</span>        <span class="k">if</span> <span class="n">done</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-15'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-15'>#</a>
            </div>
            <p>if finished, set episode information if episode is over, and reset </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">91</span>            <span class="n">episode_info</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;reward&quot;</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">),</span> <span class="s2">&quot;length&quot;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">)}</span>
<span class="lineno">92</span>            <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="lineno">93</span>        <span class="k">else</span><span class="p">:</span>
<span class="lineno">94</span>            <span class="n">episode_info</span> <span class="o">=</span> <span class="kc">None</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-16'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-16'>#</a>
            </div>
            <p>get the max of last two frames </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">97</span>            <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-17'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-17'>#</a>
            </div>
            <p>push it to the stack of 4 frames </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">100</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">roll</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">,</span> <span class="n">shift</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="lineno">101</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">obs</span>
<span class="lineno">102</span>
<span class="lineno">103</span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">episode_info</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-18'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-18'>#</a>
            </div>
            <h3>Reset environment</h3>
<p>Clean up episode info and 4 frame stack</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">105</span>    <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-19'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-19'>#</a>
            </div>
            <p>reset OpenAI Gym environment </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">112</span>        <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-20'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-20'>#</a>
            </div>
            <p>reset caches </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">115</span>        <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">)</span>
<span class="lineno">116</span>        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="lineno">117</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">obs</span>
<span class="lineno">118</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span> <span class="o">=</span> <span class="p">[]</span>
<span class="lineno">119</span>
<span class="lineno">120</span>        <span class="bp">self</span><span class="o">.</span><span class="n">lives</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">unwrapped</span><span class="o">.</span><span class="n">ale</span><span class="o">.</span><span class="n">lives</span><span class="p">()</span>
<span class="lineno">121</span>
<span class="lineno">122</span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-21'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-21'>#</a>
            </div>
            <h4>Process game frames</h4>
<p>Convert game frames to gray and rescale to 84x84</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">124</span>    <span class="nd">@staticmethod</span>
<span class="lineno">125</span>    <span class="k">def</span> <span class="nf">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-22'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-22'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">130</span>        <span class="n">obs</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">cvtColor</span><span class="p">(</span><span class="n">obs</span><span class="p">,</span> <span class="n">cv2</span><span class="o">.</span><span class="n">COLOR_RGB2GRAY</span><span class="p">)</span>
<span class="lineno">131</span>        <span class="n">obs</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="n">obs</span><span class="p">,</span> <span class="p">(</span><span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">),</span> <span class="n">interpolation</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">INTER_AREA</span><span class="p">)</span>
<span class="lineno">132</span>        <span class="k">return</span> <span class="n">obs</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-23'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-23'>#</a>
            </div>
            <h2>Worker Process</h2>
<p>Each worker process runs this method</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">135</span><span class="k">def</span> <span class="nf">worker_process</span><span class="p">(</span><span class="n">remote</span><span class="p">:</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">Connection</span><span class="p">,</span> <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-24'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-24'>#</a>
            </div>
            <p>create game </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">143</span>    <span class="n">game</span> <span class="o">=</span> <span class="n">Game</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-25'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-25'>#</a>
            </div>
            <p>wait for instructions from the connection and execute them </p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">146</span>    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="lineno">147</span>        <span class="n">cmd</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">remote</span><span class="o">.</span><span class="n">recv</span><span class="p">()</span>
<span class="lineno">148</span>        <span class="k">if</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;step&quot;</span><span class="p">:</span>
<span class="lineno">149</span>            <span class="n">remote</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">game</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="lineno">150</span>        <span class="k">elif</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;reset&quot;</span><span class="p">:</span>
<span class="lineno">151</span>            <span class="n">remote</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">game</span><span class="o">.</span><span class="n">reset</span><span class="p">())</span>
<span class="lineno">152</span>        <span class="k">elif</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;close&quot;</span><span class="p">:</span>
<span class="lineno">153</span>            <span class="n">remote</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="lineno">154</span>            <span class="k">break</span>
<span class="lineno">155</span>        <span class="k">else</span><span class="p">:</span>
<span class="lineno">156</span>            <span class="k">raise</span> <span class="ne">NotImplementedError</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-26'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-26'>#</a>
            </div>
            <p> Creates a new worker and runs it in a separate process.</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">159</span><span class="k">class</span> <span class="nc">Worker</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-27'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-27'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">164</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seed</span><span class="p">):</span>
<span class="lineno">165</span>        <span class="bp">self</span><span class="o">.</span><span class="n">child</span><span class="p">,</span> <span class="n">parent</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Pipe</span><span class="p">()</span>
<span class="lineno">166</span>        <span class="bp">self</span><span class="o">.</span><span class="n">process</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker_process</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">seed</span><span class="p">))</span>
<span class="lineno">167</span>        <span class="bp">self</span><span class="o">.</span><span class="n">process</span><span class="o">.</span><span class="n">start</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>